home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 1.iso / games / wordy313.zip / XTRACT.C < prev    next >
C/C++ Source or Header  |  1995-04-29  |  8KB  |  310 lines

  1. /**************************************************************************/
  2. /*                             Extract Utility                        */
  3. /*                                                            */
  4. /*                                 M\Cooper                            */
  5. /*                        3425 Chestnut Ridge Rd.                        */
  6. /*                        Grantsville, MD 21536-9801                    */
  7. /*                        --------------------------                    */
  8. /*                        Email:  thegrendel@aol.com                    */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package             */
  11. /*                                                            */    
  12. /**************************************************************************/
  13.  
  14.  
  15. /**********************************WORDTEST********************************/
  16. /*       Function tests if word is constructible from Letterset            */
  17. /*                 Args in: char *letterset, char *word                          */
  18. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  19. /**************************************************************************/
  20.  
  21. #include <conio.h>
  22. #include "srch.h"
  23.  
  24.  
  25. #define FILE_OPENING_ERROR 3
  26. #define FILENAME_MAXLEN 8
  27. #define CR "\n"
  28. #define FILE_SUFFIX ".xtr"
  29. #define MAXLEN 30
  30. #define LINE_LEN 80
  31. #define NOARGS 1
  32. #define INCREMENT 1
  33. #define SPACE ' '
  34. #define NOT "~!"
  35.  
  36. #define BUFFERSIZE 8192
  37. /*******8K buffer******/
  38.  
  39. char ad[] =
  40. "XTRACT tool by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536";
  41.  
  42. typedef enum { FALSE, TRUE } Boolean;
  43.  
  44. void getword( char *lset, char *nlset, char *wrdfile );
  45. void center( char *strng );
  46. void parse( char *argument, char *lset, char *not_letterset );
  47. Boolean wordtest( char *letterset, char *word );
  48. char *nw_test( char *nlset, char *word );
  49.  
  50.  
  51. void main( int argc, char **argv )
  52. {
  53.  
  54.    char letterset[ MAXLEN ],
  55.        n_letterset[ MAXLEN ],
  56.        input_set[ MAXLEN ],
  57.     wfile [ MAXLEN ];  
  58.  
  59.      strcpy ( n_letterset, NULL );
  60.  
  61.      if( argc == NOARGS )
  62.         {
  63.         clrscr();
  64.         puts( "Enter a LETTERSET to test [~ or ! for excluded letters]..." );
  65.         gets( input_set );
  66.         parse( input_set, letterset, n_letterset );
  67.  
  68.       printf( "\n\n" );
  69.       puts( "Enter the name of the word file to search... " );
  70.       gets( wfile );
  71.  
  72.         }
  73.      else
  74.         parse( argv[1], letterset, n_letterset );
  75.  
  76.       if( argc == NOARGS + 1 )
  77.          strcpy( wfile, "word.lst" );
  78.       else
  79.          if( argc == NOARGS + 2 )
  80.             strcpy( wfile, argv [2] );
  81.  
  82.      getword( letterset, n_letterset, wfile );
  83. }
  84.  
  85.  
  86.  
  87. Boolean wordtest( char *letterset, char *word )
  88. {
  89.    char temp [MAXLEN],
  90.        *t;
  91.    Boolean error_flag = TRUE;
  92.  
  93.      if( *letterset == NULL )
  94.         return( error_flag );  //All valid if no specs given
  95.  
  96.      strcpy( temp, word );  //Preserve WORD
  97.  
  98.      while( *letterset )
  99.         {
  100.         t = strchr( temp, *letterset++ );
  101.  
  102.         if( !t )
  103.           {
  104.           error_flag = FALSE;
  105.           return( error_flag );
  106.           } 
  107.         
  108.         *t = '*';  //Remove occurrence of letter
  109.         }
  110.  
  111.         return( error_flag );
  112. }
  113.  
  114. char *nw_test( char *nlset, char *word )
  115. {
  116.    char *ptr;
  117.  
  118.      ptr = ( strpbrk( word, nlset ) );
  119.  
  120.      return( ptr );
  121. }
  122. /*************************************************************/
  123. void getword( char *letter_set, char *nlset, char *wordfile )
  124. {
  125.  
  126.     char    l_set [ MAXLEN ],
  127.         word [ MAXLEN ],
  128.         tempstr [ MAXLEN + 1 ],
  129.         targetfile [ MAXLEN ],
  130.         bar [ LINE_LEN + 1 ],
  131.         double_bar [ LINE_LEN + 1 ];
  132.  
  133.     FILE *fptr,
  134.         *tfile;
  135.     int fnamelen;
  136.     long wcount = 0L;
  137.  
  138.        memset( bar, '-', LINE_LEN );
  139.        *( bar + LINE_LEN ) = NULL;
  140.        memset( double_bar, '=', LINE_LEN );
  141.        *( double_bar + LINE_LEN ) = NULL;
  142.  
  143.        /*************opening credits*************/
  144.        clrscr();
  145.        printf( double_bar );
  146.        strcpy( tempstr, ad );
  147.        center ( tempstr );
  148.        printf( tempstr );
  149.        printf( CR );
  150.        printf( double_bar );
  151.        printf( CR );
  152.        /****************************************/
  153.  
  154.  
  155.        strcpy ( l_set, letter_set );
  156. //       strcat ( letter_set, CR );
  157.  
  158.        /*   Create name of file to store derived words in   */
  159.        /*********************************************************/
  160.        fnamelen = strlen( l_set );
  161.  
  162.        if( fnamelen > 0 )
  163.          {
  164.          if( fnamelen  > FILENAME_MAXLEN )
  165.             fnamelen = FILENAME_MAXLEN;
  166.          strncpy( targetfile, l_set, fnamelen );
  167.          *( targetfile + fnamelen ) = NULL;
  168.             //NULL-terminate string, so strcat works, ha, ha.
  169.          }
  170.        else
  171.          strcpy( targetfile, "not" );
  172.  
  173.        strcat( targetfile, FILE_SUFFIX );
  174.        /*********************************************************/
  175.  
  176.        if( !( fptr = fopen( wordfile, "rt" ) ) )
  177.          {
  178.          printf( "\7\7\7Cannot open Wordfile!" );
  179.          exit( FILE_OPENING_ERROR );
  180.          }
  181.       if( setvbuf( fptr, NULL, _IOFBF, 2 * BUFFERSIZE ) )
  182.          exit( FILE_OPENING_ERROR );
  183.  
  184.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  185.          {
  186.          printf( "\7\7\7Cannot open file to save words in!" );
  187.          exit ( FILE_OPENING_ERROR + 1 );
  188.          }
  189.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  190.          exit( FILE_OPENING_ERROR );
  191.  
  192.        /**************'Wait' Message************/
  193.        printf( CR CR );
  194.        printf( "WORKING...\n\n" );
  195.        printf( "This will take from 10 seconds or less [fast 486 machine]\n" );
  196.        printf( "to 5 minutes or more [slow 8088 with old hard drive].\n\n" );
  197.        printf( "Now searching word file %s\nand writing file of valid words containing -%s-", 
  198.           wordfile, letter_set );
  199.  
  200.         if( *nlset )
  201.           printf( " but NOT -%s-", nlset );
  202.  
  203.         printf( ".\n\n" );
  204.        /*****************************************/
  205.  
  206.  
  207.  
  208.  
  209.  
  210.        sprintf( tempstr, "Words containing: %s", strupr( l_set ) );
  211.        if( *nlset )
  212.             {
  213.             strcat( tempstr, ", but NOT -" );
  214.             strcat( tempstr, nlset );
  215.             strcat( tempstr, "-" );
  216.             }
  217.        strcat( tempstr, CR );
  218.  
  219.        center( tempstr );
  220.        fprintf( tfile, double_bar );
  221.     fprintf( tfile, CR );
  222.        fprintf( tfile, tempstr );
  223.        fprintf( tfile, double_bar );
  224.        fprintf( tfile, CR );
  225.  
  226.  
  227.          /*********************Main Loop*************/     
  228.           while( fgets( word, MAXLEN, fptr ) != NULL )
  229.  
  230.             if( wordtest( letter_set, word ) )
  231.                if( !nw_test( nlset, word ) )
  232.                  {
  233.                  fprintf( tfile, "%s", word );
  234.                  wcount++;
  235.                  }
  236.           /*******************************************/
  237.  
  238.           fprintf( tfile, bar );
  239.           sprintf( tempstr, "%ld words contain %s.",
  240.                  wcount, l_set );
  241.           if( *nlset )
  242.             {
  243.             strcat( tempstr, ".. but NOT --- " );
  244.             strcat( tempstr, nlset );
  245.             strcat( tempstr, "." );
  246.             }
  247.  
  248.           center( tempstr );              
  249.           fprintf( tfile, tempstr );
  250.           fprintf( tfile, "\n\n" );
  251.  
  252.           center( ad );
  253.           fprintf( tfile, ad );
  254.  
  255.           fcloseall();
  256.  
  257.           sprintf( tempstr,
  258.                  "The file %s has %ld words containing %s.",
  259.                  targetfile, wcount, l_set );
  260.           if( *nlset )
  261.             {
  262.             strcat( tempstr, ".. but NOT --> " );
  263.             strcat( tempstr, nlset );
  264.             strcat( tempstr, "." );
  265.             }
  266.  
  267.           center( tempstr );
  268.           printf( CR CR );
  269.           printf( tempstr );
  270.           printf( "\7" );  //Bell
  271.  
  272. }
  273.  
  274.  
  275.  
  276. void center( char *str )
  277. {
  278.    int padding;
  279.    char st [ LINE_LEN + INCREMENT ];
  280.  
  281.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  282.      memset( st, SPACE, padding );
  283.      *( st + padding ) = NULL;  //Terminate string
  284.      strcat( st, str );
  285.      strcpy( str, st );
  286.  
  287.      return;
  288. }
  289.  
  290. void parse( char *arg, char *letterset, char *nls )
  291. {
  292.    char *p;
  293.  
  294.      if( *arg == '~' || *arg == '!' )
  295.         {
  296.         *letterset = NULL;
  297.         strcpy( nls, ++arg );
  298.         return;
  299.         }
  300.  
  301.      p = strtok( arg, NOT );
  302.      strcpy( letterset, p );
  303.  
  304.      p = strtok( NULL, NULL );
  305.      if( *p )
  306.         strcpy( nls, p );
  307.  
  308.      return;
  309. }
  310.